__author__ = 'anton.mcconville@gmail.com'

import wsgiref.handlers
import cgi
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Field( db.Model ):
    name = db.StringProperty( multiline=True )
    code = db.StringProperty( multiline=True )
    address = db.StringProperty(multiline=True)
    lat = db.StringProperty(multiline=True)
    long = db.StringProperty(multiline=True)

class FieldList(webapp.RequestHandler):

  def get(self):
      
    self.response.headers['Content-Type'] = 'text/xml'
    self.response.out.write('<?xml version="1.0"?>\n')
    self.response.out.write( '<fields>\n')
    
    fields = db.GqlQuery("SELECT * FROM Field")

    for field in fields:
      if field.name:
        self.response.out.write('   <field name=\"%s\" address=\"%s\" lat=\"%s\" long=\"%s\"/>\n' % ( field.name, field.address, field.lat, field.long ) )
      else:
        self.response.out.write('Big problems!:')
    
    self.response.out.write( '</fields>\n')

   # self.response.out.write( '   <field name = %s address = %s lat = %s long = %s'%( ) )
    
def main():
  application = webapp.WSGIApplication([('/.*', FieldList),], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()